home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / Open Me for REALbasic 3 / REALbasic 3.2 / Example Projects / Applications / Simon 1.0 / source text < prev   
Text File  |  1998-05-13  |  6KB  |  313 lines

  1. Main.blink:
  2. Sub blink(x as oval)
  3.   // Makes the Music button blink when hit by the user
  4.   // There are probably better ways to do this
  5.   // But this one is OK for this game
  6.   
  7.   dim tmp as color
  8.   dim height as integer
  9.   
  10.   // record current bordercolor of Music button
  11.   tmp = x.bordercolor
  12.   
  13.   // select note according to the Music button number
  14.   height = x.index*4+60
  15.   
  16.   // play note
  17.   note.playNote height,127
  18.   
  19.   // change bordercolor to white
  20.   x.bordercolor = rgb(255,255,255)
  21.   
  22.   // absolutely required for correct visual effect
  23.   x.refresh
  24.   
  25.   // just for eye's catching
  26.   wait 8
  27.   
  28.   // restore previous color
  29.   x.bordercolor = tmp
  30.   
  31.   // absolutely required for correct visual effect
  32.   x.refresh
  33.   
  34.   // stop playing note
  35.   note.playNote height,0
  36.   
  37. End Sub
  38.  
  39. Main.init:
  40. Sub init()
  41.   // Initialisation method
  42.   dim i as integer
  43.   //creation of game buttons
  44.   for i = 0 to 5
  45.     simon(i) = new oval1
  46.   next
  47.   // Buttons are organised as an hexagon 
  48.   // Their colors are the 3 primary and 
  49.   // the 3 secondary ones 
  50.   
  51.   // First one is blue
  52.   simon(0).fillcolor = rgb(0,0,255)
  53.   simon(0).top = 30
  54.   simon(0).left = 135
  55.   
  56.   // Second one is Magenta
  57.   simon(1).fillcolor = rgb(255,0,255)
  58.   simon(1).top = 80
  59.   simon(1).left = 222
  60.   
  61.   // Third one is Red
  62.   simon(2).fillcolor = rgb(255,0,0)
  63.   simon(2).top = 180
  64.   simon(2).left = 222
  65.   
  66.   // Fourth one is Yellow
  67.   simon(3).fillcolor = rgb(255,255,0)
  68.   simon(3).top = 230
  69.   simon(3).left = 135
  70.   
  71.   // Fifth one is Green
  72.   simon(4).fillcolor = rgb(0,255,0)
  73.   simon(4).top = 180
  74.   simon(4).left = 48
  75.   
  76.   // Sixth one is Cyan
  77.   simon(5).fillcolor = rgb(0,255,255)
  78.   simon(5).top = 80
  79.   simon(5).left = 48
  80.   
  81.   // Max length of music sequence
  82.   numseq = 0
  83.   PlayInProgress = false
  84. End Sub
  85.  
  86. Main.Start:
  87. Sub Start()
  88.   // Method activated by depressing NewPlay Button
  89.   dim i as integer
  90.   // Initialisation of a random musical sequence
  91.   // 20 notes in sequence is already pretty hard
  92.   // to remember (at least for me !)
  93.   for i = 0 to 19 
  94.     seq(i) = floor(rnd * 6)
  95.   next
  96.   // A new game has started
  97.   PlayInProgress =true
  98.   // No success yet
  99.   Score = 0
  100.   UpdScore Score
  101.   // Start of Sequence
  102.   Numseq = 0
  103.   CurrSeq = 0
  104.   // Play First note
  105.   PlaySeq 0
  106. End Sub
  107.  
  108. Main.wait:
  109. Sub wait(x as integer)
  110.   // Just my own implementation of HyperCard
  111.   // wait n x ticks
  112.   // Didn't find it in the documentation
  113.   
  114.   dim t as integer
  115.   t = ticks
  116.   do
  117.   loop until ((ticks-t) > x)
  118. End Sub
  119.  
  120. Main.PlaySeq:
  121. Sub PlaySeq(num as integer)
  122.   // Plays a whole sequence of notes 
  123.   // while showing buttons blink
  124.   dim i as integer
  125.   for i = 0 to num 
  126.     blink simon(seq(i))
  127.     wait 20
  128.   next
  129. End Sub
  130.  
  131. Main.check:
  132. Function check(btn as integer) As Boolean
  133.   // Checks if the button hit is the right one 
  134.   // in the sequence
  135.   return (btn = seq(CurrSeq))
  136.   
  137. End Function
  138.  
  139. Main.good:
  140. Sub good()
  141.   // Button hit is the right one
  142.   
  143.   if CurrSeq >= numseq then 
  144.     // We have completed the right music sequence
  145.     
  146.     ouais.play // French yeah :^)
  147.     
  148.     // Didn't find something like Hypercard
  149.     // wait until the sound is done 
  150.     wait 100 
  151.     
  152.     // increase score
  153.     score = CurrSeq +1
  154.     
  155.     // Update score
  156.     UpdScore score
  157.     
  158.     // increase sequence number
  159.     numseq = numseq+1
  160.     
  161.     // Resets current index for the next sequence
  162.     CurrSeq = 0
  163.     
  164.     // Have we reach the maximum score ?
  165.     if numseq = 20 then 
  166.       
  167.       // Play another sound 
  168.       // NB : I borrowed this one to Civ II
  169.       fin.Play
  170.       
  171.       // Game is over
  172.       PlayInProgress = false
  173.       numseq = 0
  174.       
  175.     else
  176.       // Play the next music sequence
  177.       PlaySeq numseq
  178.     end if
  179.     
  180.   else  
  181.     // We are in the middle of a sequence
  182.     CurrSeq = CurrSeq+1
  183.   end if
  184. End Sub
  185.  
  186. Main.bad:
  187. Sub bad()
  188.   // You loose !!
  189.   iark.play
  190.   
  191.   // Game is over
  192.   PlayInProgress = false
  193.   
  194. End Sub
  195.  
  196. Main.UpdScore:
  197. Sub UpdScore(n as integer)
  198.   // Update of current score in main window
  199.   // Both, in an EditField in the center of 
  200.   // the window
  201.   affscore.text = str(n)
  202.   
  203.   // And in a scrollbar just below the EditField
  204.   // Quite unuseful but I just tried it and 
  205.   // it worked from the first time. Coool ! :^)
  206.   scorebar.value = n
  207. End Sub
  208.  
  209. Main.MouseDown:
  210. Function MouseDown(X As Integer, Y As Integer) As Boolean
  211.   // Main event to be tracked down in the main window
  212.   // Detects music buttons hits and reacts accordingly
  213.   
  214.   dim i, Button as integer
  215.   dim ok as boolean
  216.   
  217.   // temporary object
  218.   dim tg as oval
  219.   
  220.   // Button is initialised to nothing
  221.   Button = -1 
  222.   
  223.   if PlayInProgress then
  224.     // we check if the mouse location is within
  225.     // each music button rectangle
  226.     for i = 0 to 5
  227.       tg = simon(i)
  228.       ok = (X > tg.left) and (X < (tg.left + tg.width))
  229.       ok = ok and (Y > tg.top) and (y < (tg.top + tg.height))
  230.       
  231.       if ok then 
  232.         // We hit a Music Button so we give
  233.         // feedback to the user 
  234.         blink tg
  235.         
  236.         // and record the button number
  237.         Button = i
  238.         
  239.       end if
  240.     next
  241.     // At the end ofthis loop we should have 
  242.     // either one music button ( 0 to 5) 
  243.     // or no button (-1)
  244.     if Button <> -1 then 
  245.       // One music button has been hit
  246.       // Is it the right one ?
  247.       if check(Button) then 
  248.         
  249.         // Yeah got it!
  250.         good
  251.         
  252.       else
  253.         // Rats !! 
  254.         bad
  255.         
  256.       end if
  257.       // Oops !! clicked outside all Music Buttons
  258.     end if 
  259.     
  260.   else 
  261.     // No game is in progress so we just beep
  262.     beep
  263.     
  264.   end if
  265.   
  266. End Function
  267.  
  268. Main.Close:
  269. Sub Close()
  270.   // This application has no multidocument
  271.   // capability, so I quit upon close 
  272.   quit
  273.   
  274. End Sub
  275.  
  276. Main.EnableMenuItems:
  277. Sub EnableMenuItems()
  278.   AppleAbout.enabled = true
  279.   FileQuit.enabled =true
  280. End Sub
  281.  
  282. Main.Open:
  283. Sub Open()
  284.   // Main Window is not shown during initialisation
  285.   main.hide
  286.   
  287.   init
  288.   main.show
  289. End Sub
  290.  
  291. Main.bye.Action:
  292. Sub Action()
  293.   // User has depressed 'Quit' button
  294.   quit
  295. End Sub
  296.  
  297. Main.np.Action:
  298. Sub Action()
  299.   // User has depressed 'New Game' button
  300.   start
  301. End Sub
  302.  
  303. About.MouseDown:
  304. Function MouseDown(X As Integer, Y As Integer) As Boolean
  305.   Close
  306. End Function
  307.  
  308. About.KeyDown:
  309. Function KeyDown(Key As String) As Boolean
  310.   Close
  311. End Function
  312.  
  313.